import request from '@/utils/request'
import { Message } from 'element-ui'

export default{
    /**
     * 清除表单验证
     * {表单ref值} formName 
     */
    clearcheck(self,formName){
        if(self.$refs[formName]){
            self.$refs[formName].clearValidate()
        } 
    },
    /** 日期格式化 */
    dateFormat(dateData) {
        var date = new Date(dateData)
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        var d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        const time = y + '-' + m + '-' + d
        return time
    },
    /** 月份格式化
     *  上个月
     *  */  
     monthFormatBefore(dateData) {
        var date = new Date(dateData);
        var y = date.getFullYear()
        var m = date.getMonth() 
        m = m < 10 ? ('0' + m) : m
        const time = y + '-' + m
        return time
    },
    /** 月份格式化 */
    monthFormat(dateData) {
        var date = new Date(dateData);
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        const time = y + '-' + m
        return time
    },
    /** 时间格式化 */
    timeFormat(date) {
        var h = date.getHours();
        var m = date.getMinutes();
        var s = date.getSeconds();
        h = h < 10 ? ('0' + h) : h;
        m = m < 10 ? ('0' + m) : m;
        s = s < 10 ? ('0' + s) : s;
        const time= h + ':' + m + ':' + s;
        return time;
    },
    /** 日期时间格式化 */
    dateTimeFormat(dateData){
        var date = new Date(dateData)
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        m = m < 10 ? ('0' + m) : m
        d = d < 10 ? ('0' + d) : d
        var h = date.getHours();
        var mi = date.getMinutes();
        var s = date.getSeconds();
        h = h < 10 ? ('0' + h) : h;
        mi = mi < 10 ? ('0' + mi) : mi;
        s = s < 10 ? ('0' + s) : s;
        const time= y + '-' + m + '-' + d + ' ' + h + ':' + mi + ':' + s;
        return time
    },
    /** 日期时间格式化 */
    dateHourFormat(dateData){
        var date = new Date(dateData)
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        m = m < 10 ? ('0' + m) : m
        d = d < 10 ? ('0' + d) : d
        var h = date.getHours();
        // var mi = date.getMinutes();//change--焦誉暄
        // var s = date.getSeconds();//change--焦誉暄
        h = h < 10 ? ('0' + h) : h;
        const time= y + '-' + m + '-' + d + ' ' + h + ':00:00';
        return time
    },
    /** 日期时间格式化 */
    dateOnlyHourFormat(dateData){
        var date = new Date(dateData)
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        m = m < 10 ? ('0' + m) : m
        d = d < 10 ? ('0' + d) : d
        var h = date.getHours();
        // var mi = date.getMinutes();//change--焦誉暄
        // var s = date.getSeconds();//change--焦誉暄
        h = h < 10 ? ('0' + h) : h;
        const time= y + '-' + m + '-' + d + ' ' + h;
        return time
    },
    // 处理公共
    downloadExcel(res, fileName) {
        let blob = new Blob([res], {type: 'application/vnd.ms-excel'});
        let url = window.URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = url;
        a.download = decodeURI(fileName);
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();  
        URL.revokeObjectURL(a.href);
        document.body.removeChild(a);
    },
    // 导出
    exportExcel(url,param,errMsg='导出数据失败。'){
        return request({
          url: `/zy-api/${url}`,
          method: 'post',
          data: param,
          responseType: 'blob'
        }).then(res => {
            if(res.type === 'application/json'){
              Message({
                message: errMsg,
                type: 'error'
              })
            } else {
              let fileName = localStorage.getItem('fileName')
              this.downloadExcel(res, fileName)
            } 
        })
    },
    // 下载文件
    downloadFile(url) {
        const a = document.createElement("a");
        a.href = url;
        // a.target = '_blank';
        // a.download = decodeURI(fileName);
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();  
        URL.revokeObjectURL(a.href);
        document.body.removeChild(a);
    },
    cutOutURL(url) {
        var urlReg = /http:\/\/([^\/]+)\//i
        let strArr = urlReg.exec(url);
        var URL = url.split(strArr[0]);
        return location.origin + '/zy-api/' + URL[1]
    },
    cutOutURLNotDomainName(url) {
        var urlReg = /http:\/\/([^\/]+)\//i
        let strArr = urlReg.exec(url);
        var URL = url.split(strArr[0]);
        return URL[1]
    },

     // 截取 字符串最后一位
     lastStr(str){
        let index = str.lastIndexOf('\/')
        str = str.substring(index+1,str.length)
        return str
      },
}